<?php
// 随机生成验证码图片
session_start();
header("content-type:image/jpeg"); //设置生成图片
$image = imagecreatetruecolor(100, 40);//设置画布
$bgcolor = imagecolorallocate($image, rand(200,255), rand(200,255), rand(200,255));
imagefill($image, 0, 0, $bgcolor);

//画干扰点
for($i=0;$i<100;$i++){
	$pointcolor = imagecolorallocate($image, rand(150,255), rand(150,255), rand(150,255));
	imagesetpixel($image, rand(0,100), rand(0,30), $pointcolor);
}
//画干扰线
for($i=0;$i<3;$i++){
	$linecolor = imagecolorallocate($image, rand(0,255), rand(0,255), rand(0,255));
	imageline($image,0,rand(0,30),100, rand(0,30), $linecolor);
}

//生成验证字符串
$str = "qwertyuiopasdfghjklzxcvbnm1234567890";
$str = str_shuffle($str);
$yzm_str = substr($str,0,4);
$_SESSION['code'] = $yzm_str;
//画验证码字符串
$strcolor = imagecolorallocate($image, rand(50,150), rand(50,150), rand(50,150));
imagettftext($image, 25, rand(8,10), rand(5,15), rand(30,35), $strcolor, __DIR__."\arial.ttf", $yzm_str);

imagejpeg($image);